package and.roid.box;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import android.R.color;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BoxView extends SurfaceView implements SurfaceHolder.Callback, Runnable {
	private SurfaceHolder holder;
	private Thread thread;

	private float windowWith, windowHeight;
	private BoxActivity boxActivity;

	private static final int gamen_x = 5, gamen_y = 5, gamen_col = Color.BLACK;
	
	private List<BoxOb> listBoxs = new ArrayList<BoxOb>();
	private static final int BOX_WH = 100, BOX_D = 360;
	private int rectWith;
	private int rectHegith;
	
	public BoxView(Context context) {
		super(context);
		holder = getHolder();
		holder.addCallback(this);
		holder.setFixedSize(getWidth(), getHeight());

		init(context);
	}

	public void init(Context context) {
		boxActivity = (BoxActivity) context;
		rectWith = (int) windowWith - 5;
		rectHegith = (int) windowHeight - 35;
	}

	public void run() {
		Canvas canvas;
		Paint paint = new Paint();
		paint.setAntiAlias(true);

		while (thread != null) {
			if (boxActivity.isDeleteBox() == true) {
				boxActivity.setBoxDeleteFlg();
				listBoxs.clear();
			}
			canvas = holder.lockCanvas();
			canvas.drawColor(boxActivity.getcolor());

			paint.setStyle(Paint.Style.STROKE);
			paint.setColor(Color.BLACK);
			canvas.drawRect(new Rect(gamen_x, gamen_y, rectWith, rectHegith), paint);
			for(BoxOb boxOb : listBoxs){
				boxOb.draw(canvas, paint);
				boxOb.move(gamen_x, gamen_y, rectWith, rectHegith);
			}

			paint.setTextSize(30);
			paint.setColor(color.black);
			canvas.drawText("reset:" + boxActivity.isDeleteBox(), 10, 50, paint);

			holder.unlockCanvasAndPost(canvas);

			try {
				Thread.sleep(50);
			} catch (Exception e) {
			}
		}
	}

	public boolean onTouchEvent(MotionEvent event) {
		int action = event.getAction();
		int x = (int) event.getX();
		int y = (int) event.getY();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			break;
		case MotionEvent.ACTION_UP:
			if (x < windowWith - 95 && y < windowHeight - 95) {

				Random r = new Random(new Date().getTime());
				int wh = r.nextInt(BOX_WH) + 5;
				int d = r.nextInt(BOX_D);
				int col = Color.rgb(r.nextInt(240) + 10, r.nextInt(240) + 10, r.nextInt(240) + 10);
				int vxy = r.nextInt(20) + 5;
				listBoxs.add(new BoxOb(x, y, wh, d, vxy, col));

			}
			break;
		}
		return true;
	}

	public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
		windowWith = arg2;
		windowHeight = arg3;
	}

	public void surfaceCreated(SurfaceHolder arg0) {
		thread = new Thread(this);
		thread.start();
	}

	public void surfaceDestroyed(SurfaceHolder arg0) {
		thread = null;
	}

}